/**
 * class name: split$,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://hcjs.googlecode.com/svn/trunk/hc.js,
 * update: http://hcjs.googlecode.com/svn/trunk/split.js
 *
 */
function split$(){
    var A = arguments,
        _ = this;
    _ = ext$( _, ext$( {
        id : 'split'+ now$(3),
        elem : me.body,
        splitcolor : '#abf',
        width : 200,
        minWidth : 150,
        defWidth : 200,
        maxWidth : 500,
        nav : '',
        data : '',
        ico : { 'close' : '/icos/ishut.gif' }
        }, A[0] ) );

    _.uitype = 'split$';

    _.reset$ = function(elem){
        if ( elem&&elem.nodeType ) {
            _.elem = elem;
        }

        if ( !_.elem.hasChildNodes() ) {
            _.elem.appendChild(_.$);
        }

        if ( typeof _.data=='function' ) {
            _.xtar = _.data.apply(_.tar, [_.mnu]);

            if ( typeof _.xtar=='string' ) {
                _.tar.innerHTML = _.xtar;
            }
        } else if ( typeof _.data=='string' ) {
            _.tar.innerHTML = _.data;
        } else if ( _.data.nodeType ) {
            _.tar.appendChild(_.data);
        }

        if ( typeof _.nav=='function' ) {
            _.xmnu = _.nav.apply(_.mnu, [_.tar]);

            if ( typeof _.xmnu=='string' ) {
                _.mnu.innerHTML = _.xmnu;
            }
        } else if ( typeof _.nav=='string' ) {
            _.mnu.innerHTML = _.nav;
        } else if ( _.nav.nodeType ) {
            _.mnu.appendChild(_.nav);
        }

        _.width = _.defWidth;
        _.togg$(true);

    return _;
    };

    _.stop$ = function(){
        try {
            unbind$(me,'mousemove', _.resize$);
            unbind$(me,'mouseup', _.stop$);

            if ( $$.msie ) {
                me.onselectstart = function(){;};
            }
        } catch (e) {}
    };

    _.resizeto$ = function(w){
        if ( w>=_.minWidth && w<=_.maxWidth ) {
            _.width = w;
            _.togg$(true);
        }
    return _;
    };

    _.resize$ = function(ev){
        var ev = evt$(ev);
        _.width = ev.X-_.zx<_.minWidth ? _.minWidth : ev.X-_.zx>_.maxWidth ? _.maxWidth : ev.X-_.zx;
        _.togg$(true);
    return _;
    };

    _.toresize$ = function(ev){
        if ( $$.msie ) {
            me.onselectstart = function(){
                me.selection.empty();
                event.returnValue = false;
            };
        }

        var ev = evt$(ev);
        _.zx = ev.X-_.width;

        bind$(me,'mousemove',_.resize$);
        bind$(me,'mouseup',_.stop$);

    return _;
    };

    _.togg$ = function(f){
        if ( f ) {
            _.mnu.style.width = _.width +'px';
            _.mnu.style.display = 'block';
            _.hv.style.left = _.width +'px';
            _.tar.style.left = (_.width+6) +'px';
            _.tar.style.width = (_.pz.width-_.width-6) +'px';
        } else {
            _.mnu.style.display = 'none';
            _.hv.style.left = 0;
            _.tar.style.left = '6px';
            _.tar.style.width = (_.pz.width-6) +'px';
        }

    return _;
    };

    _.init$=function(){
        _.defWidth = _.width;
        _.pz = pos$(_.elem);

        _.$ = el$('div').class$('split').appendTo$(_.elem);
        _.$.id = _.id = !$(_.id) ? _.id : '_split_'+ _.id;

        with (_.$.style) {
            position = 'absolute';
            top = 0;
            left = 0;
            width = _.pz.width +'px';
            height = '100%';
        }

        _.mnu = el$('div').class$('sl').appendTo$(_.$);
        _.mnu.style.cssText = 'width:'+ _.width +'px;height:100%;';
        _.mnu.id = _.id +'_left';

        _.tar = el$('div').class$('sr').appendTo$(_.$);
        _.tar.style.cssText = 'width:'+ (_.pz.width-_.width-6) +'px;left:'+ (_.width+6) +'px;height:100%;';
        _.tar.id = _.id +'_right';

        _.hv = el$('div').class$('hv').appendTo$(_.$);
        _.hv.style.cssText = 'left:'+ _.width +'px;background:'+ _.splitcolor +';line-height:100%;';

        _.hv.ondblclick = function(){
            _.togg$( _.hv.offsetLeft<_.width );
            window.getSelection && window.getSelection().removeAllRanges();
        };

        bind$(_.hv,'mousedown',_.toresize$);

        if ( typeof _.data=='function' ) {
            _.xtar = _.data.apply(_.tar, [_.mnu]);
            if ( typeof _.xtar=='string' ) {
                _.tar.innerHTML = _.xtar;
            }
        } else if ( typeof _.data=='string' ) {
            _.tar.innerHTML = _.data;
        } else if ( _.data.nodeType ) {
            _.tar.appendChild(_.data);
        }

        if ( typeof _.nav=='function' ) {
            _.xmnu = _.nav.apply(_.mnu, [_.tar]);
            if ( typeof _.xmnu=='string' ) {
                _.mnu.innerHTML = _.xmnu;
            }
        } else if ( typeof _.nav=='string' ) {
            _.mnu.innerHTML = _.nav;
        } else if ( _.nav.nodeType ) {
            _.mnu.appendChild(_.nav);
        }

    return _;
    };

}